package com.wdl.fmnew.core.mvp.rxlife.impl;

import android.support.annotation.CheckResult;
import android.support.annotation.NonNull;

import com.trello.rxlifecycle2.LifecycleTransformer;
import com.trello.rxlifecycle2.RxLifecycle;
import com.wdl.fmnew.core.mvp.rxlife.RxLifeEvent;

import io.reactivex.Observable;
import io.reactivex.subjects.BehaviorSubject;

/**
 * Created by wdl on 2017/9/18.
 */

public class BaseRxLifeModel {

    protected final BehaviorSubject<RxLifeEvent> lifecycleSubject = BehaviorSubject.create();


    @NonNull
    @CheckResult
    public final <T> LifecycleTransformer<T> bindUntilEvent(@NonNull RxLifeEvent event) {
        return RxLifecycle.bindUntilEvent(lifecycleSubject, event);
    }

    /**
     * 获取添加生命控制的封装
     *
     * @param observable
     * @param event
     * @param <T>
     * @return
     */
    protected <T> Observable<T> getComposeObservable(Observable<T> observable, RxLifeEvent event) {
        return observable.compose(this.<T>bindUntilEvent(event));
    }

    protected void onDestory() {
        lifecycleSubject.onNext(RxLifeEvent.DESTROY);
    }
}
